<?php

class LiteParamCallback extends LiteCallback {

	/**
	 * @var array
	 */
	private $params = array();

	/**
	 * @param callback $callback
	 * @param array $params
	 */
	function __construct($callback, $params = null){
		parent::__construct($callback);
		$this->params = $params;
	}

	/**
	 * Crea una copia del callback y sus argumentos
	 * 
	 * <code>
	 * $cb = new LiteParamCallback( 'var_dump', array(1, 'param1', date('r')));
	 * $cb->call();
	 *
	 * $fn = $cb->getCallback();
	 * $fn(); // $var_dump(1, 'param1', date('r'));
	 *
	 * </code>
	 * @return callback
	 */
	function getCallback(){
		
		if(0 === count($this->params)) {
			return parent::getCallback();
		}
		
		$params = base64_encode(serialize($this->params));
		$callback = base64_encode(serialize(parent::getCallback()));
		
		return create_function('', "return call_user_func_array(unserialize(base64_decode('{$callback}')), unserialize(base64_decode('{$params}')));");
	}

	/**
	 * Invoca el callback
	 * 
	 * @return mixed
	 */
	function call(){
		return call_user_func_array(parent::getCallback(), $this->params);
	}
}
